<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%@ include file="/common/verfy.jsp"%>
<%@ include file="/common/operatehistory.jsp"%>
<%
    // 查询医生信息
    String date = input.getDesc("date", "");
    String deptId = input.getDesc("dept_id", "-1");
    String branch = input.getDesc("branch", "-1");
    appType = input.getDesc("app_type", "-1");
    Where where = new Where();
	if (!"-1".equals(branch))
		where.and("branch=", branch);
    List<Map> expertIds = null;
    Map resultMap = new HashMap();
    if ("section".equals(appType)) {
        expertIds = QBuilder.db("basedb").select("new map(s.doctorId as expert_id, s.doctorName as name, s.registryFee as registry_fee)").from("dept_link as dl, Schedule as s").where(where.and("dl.hospital=", hospital).and("dl.status = ", STATUS_ENABLE).and("dl.dept_id=", deptId).and("dl.dept_type=", DEPT_EXPERT).and("s.registerType=", EXPERT_NO).and("s.hospital=", hospital).and("s.status = ", STATUS_ENABLE).and("dl.other_id = s.deptId")).group("s.doctorId").query().getResultObj();
    } else {
        if (!"".equals(date)) {
            where.and("s.date=", date);
        }
        expertIds = QBuilder.db("basedb").select("new map(s.doctorId as expert_id, s.doctorName as name)").from("Schedule as s").where(where.and("s.deptId=", deptId).and("s.registerType=", EXPERT_NO).and("s.hospital=", hospital).and("s.status = ", STATUS_ENABLE)).group("s.doctorId").query().getResultObj();

        // 根据xml配置文件读取
        List<Map> normal = new ArrayList<Map>();
        if ("1".equals(checkSource(application.getRealPath("/") + appType + "/" + hospital + "/conf.xml"))) {  // 1代表实时号
            normal = QBuilder.db("basedb").select("new map(id as id, deptId as dept_id, deptName as dept_name, '1' as real)").from("Dept").where(new Where("hospital=", hospital).and("status = ", STATUS_ENABLE).and("deptId=", deptId).and("type=",DEPT_ORDINARY)).query().getResultObj();
        } else {
            String wsql = " ";
            if (!"".equals(date)) {
                wsql = " and s.date='"+date+"' ";
            }else{
                Date dat = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                wsql = " and s.date>'"+sdf.format(dat)+"' ";
            }
            // 查询普通号
            normal = QBuilder.db("basedb").queryHql("select new map(id as id, s.doctorId as expert_id, s.doctorName as name, s.deptId as dept_id, s.deptName as dept_name, s.date as date , half as half) from Schedule as s where s.deptId='"+ deptId + "' and s.registerType="+ ORDINARY_NO + " and s.hospital ="+hospital + " and s.status="+ STATUS_ENABLE + wsql +" order by date ,half ");

           //normal = QBuilder.db("basedb").select("new map(id as id, s.doctorId as expert_id, s.doctorName as name, s.deptId as dept_id, s.deptName as dept_name, s.date as date  , half as half)").from("Schedule as s").where(where.and("s.deptId=", deptId).and("s.registerType=", ORDINARY_NO).and("s.hospital=", hospital).and("s.status = ", STATUS_ENABLE)).order("date","asc").query().getResultObj();
        }
        //处理上下午
//        Map<String,String> halfMap = new HashMap<String,String>();
//        for(Map map :normal){
//            String  nowDate = map.get("date") == null ? null :  map.get("date").toString();
//            String  nowhalf = map.get("half") == null ? null :  map.get("half").toString();
//            if(nowDate == null || nowhalf == null){
//                continue;
//            }
//
//            if(halfMap.get(nowDate) != null && !nowhalf.equals(halfMap.get(nowDate))){
//                map.put("half", -1);//上下午都有
//            }else{
//                halfMap.put(nowDate,nowhalf);
//            }
//
//        }
        resultMap.put("normal", normal);
    }

    if (isEmpty(expertIds)) {
        resultMap.put("experts", new ArrayList());
	} else {
        String expertId = "'-1'";
        for (int i = 0; i < expertIds.size(); i++) {
            Map _map = expertIds.get(i);
            expertId += ",'" + Tools.getDesc(_map.get("name").toString(), "-1") + "'";
        }
        List<Map> experts = QBuilder.db("basedb").select("new map(e.id as id, e.name as name, (case when e.expertId is NULL then '' else e.expertId end) as expert_id, (case when e.avatar is NULL then '' else e.avatar end) as avatar, (case when e.specialty is NULL then '' else e.specialty end) as specialty, (case when e.title is NULL then '' else e.title end) as title)").from("Expert e").where(new Where("e.hospital=", hospital).and("e.status = ", STATUS_ENABLE).and("e.name in("+expertId+")")).query().getResultObj();
        List<Map> result = new ArrayList<Map>();
        for (int i = 0; i < expertIds.size(); i++) {
            Map _map = new HashMap();
            Map tempMap = expertIds.get(i);
            String doctorId = Tools.getDesc(tempMap.get("expert_id"), "-1");
            String name = Tools.getDesc(tempMap.get("name"), "-1");
            _map.put("expert_id", doctorId);
            _map.put("avatar", DEFAULT_AVATAR);
            _map.put("name", Tools.getDesc(tempMap.get("name"), ""));
			_map.put("registry_fee", Tools.getDesc(tempMap.get("registry_fee"), ""));
            for (int j = 0; j < experts.size(); j++) {
                Map _tempMap = experts.get(j);
                String _doctorId = (String)_tempMap.get("expert_id");
                String _name = (String)_tempMap.get("name");
                if (name.equals(_name) ) {
                    _map.put("id", Tools.getDesc(_tempMap.get("id"), ""));
                    _map.put("avatar", Tools.getDesc(_tempMap.get("avatar"), ""));
                    _map.put("title", Tools.getDesc(_tempMap.get("title"), ""));
					_map.put("specialty", Tools.getDesc(_tempMap.get("specialty"), ""));
                }
            }
            result.add(_map);
        }
        resultMap.put("experts", result);
	}
    msg.put("data", resultMap);
    Tools.encryptPrint(out, msg.toJson());
%>
